//
//  CRDataTool.m
//  CRChatRoom
//
//  Created by lanou3g on 15/11/30.
//  Copyright © 2015年 SecondGroup. All rights reserved.
//

#import "CRDataTool.h"
#import "FMDB.h"

// 缓存好友列表的数据库路径
#define filePath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"friendsCache.sqlite"]
// 缓存空间内容的数据库路径
#define spacePath [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"spaceCache.sqlite"]

@implementation CRDataTool

/**
 *  数据库管理--全局变量
 */
static FMDatabase *_db;
static FMDatabase *_spacedb;

// 做数据库的初始化
+ (void)initialize
{
    _db = [FMDatabase databaseWithPath:filePath];
    // 打开数据库
    [_db open];
        // id 主键 data 好友列表 username 用户的username 用来查询的标识
        [_db executeUpdate:@"create table if not exists friends (id integer primary key autoincrement, data blob, username text)"];
    NSLog(@"缓存好友列表的路径%@", filePath);
    
    _spacedb = [FMDatabase databaseWithPath:spacePath];
    // 打开数据库
    [_spacedb open];
    // id 主键 data 好友列表 username 用户的username 用来查询的标识
    [_spacedb executeUpdate:@"create table if not exists space (id integer primary key autoincrement, data blob, username text)"];
    NSLog(@"缓存空间内容的表的路径%@", spacePath);

    
}



/**
 *  缓存当前用户的好友列表到本地
 *
 *  @param object 缓存的数据
 */
+ (void)cacheFriendsWithObject:(id)object
{
    NSString *username = [AVUser currentUser].username;
    // 把好友列表归档为二进制===方便暴力
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
    // 缓存之前先检查--如果有就更新字段--没有就插入
    FMResultSet *result = [_db executeQuery:@"select * from friends where username = ?", username];
    if (![result next]) {  // 如果数据库中没有--就插入
        // 插入数据
        [_db executeUpdate:@"insert into friends (data, username) values (?, ?)", data, username];
    } else {  // 数据库中有记录 就更新
        [_db executeUpdate:@"update friends set data = ? where username = ?", data, username];
    }
    
}

/**
 *  查询当前用户缓存在本地的好友列表
 *  @return 返回一个好友列表信息的数组
 */
+ (NSArray *)getFriendsFromCache
{
    NSString *username = [AVUser currentUser].username;
    FMResultSet *result = [_db executeQuery:@"select data from friends where username = ?", username];
    while ([result next]) {
        NSData *data = [result dataForColumn:@"data"];
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        return array;
    }
    return nil;
}


/**
 *  缓存空间内容到本地
 */
+ (void)cacehSpaceWithObject:(id)object
{
    // 耗时操作放在子线程
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        // 缓存之前先检查是否有记录--有就更新 --没有就插入
        NSString *username = [AVUser currentUser].username;
        // 把空间内容归档为二进制===方便暴力
        NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object];
        // 缓存之前先检查--如果有就更新字段--没有就插入
        FMResultSet *result = [_spacedb executeQuery:@"select * from space where username = ?", username];
        if (![result next]) {  // 如果数据库中没有--就插入
            // 插入数据
            [_spacedb executeUpdate:@"insert into space (data, username) values (?, ?)", data, username];
        } else {  // 数据库中有记录 就更新
            [_spacedb executeUpdate:@"update space set data = ? where username = ?", data, username];
        }
    });
}

/**
 *  从本地缓存中获取空间内容
 */
+ (NSArray *)getSpaceFromCache
{
    NSString *username = [AVUser currentUser].username;
    FMResultSet *result = [_spacedb executeQuery:@"select data from space where username = ?", username];
    while ([result next]) {
        NSData *data = [result dataForColumn:@"data"];
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        return array;
    }
    return nil;

}


@end
















